Graphiques et programmes de la page graphique

Nécessite l’installation des commandes de Ben Jann grstyle et colorpalette

    ssc install grstyle
    ssc install palettes, replace
    ssc install colrspace, replace
  • Certains programmes, plutôt sur la dernière partie du document font l’objet de commentaires pour préciser les étapes de programmation
  • Certains graphiques ne sont pas présent, car redondants avec des précédents (ex graphique 23 ou les graphiques du chapitre sur les couleurs répliquées en appliquant une palette différente)

1 Générer et modifier des graphiques

1.1 Graph 1

sysuse uslifeexp.dta , clear

set scheme s2color
grstyle init
grstyle set legend, nobox stack
grstyle set plain, horizontal grid compact
grstyle set mesh,  horizontal

tw line le year, title("Espérance de vie") name(g1,replace)   ylabel(30(10)80)

tw line  le le_male le_female year, ///
   lc(*.3) title("Espérance de vie") ///
   legend(order(1 "Ensemble" 2 "Hommes" 3 "Femmes") pos(4) col(1) ring(0)) name(g2,replace) ///
   ylabel(30(10)80)

graph combine g1 g2

1.2 Graph 2

sysuse auto , clear
tw scatter price mpg if foreign==0   ///
|| scatter price mpg if foreign==1    ///  
, legend(order(1 "Domestic" 2 "Foreign")) name(g1,replace) 

tw scatter price mpg if foreign==0 , msiz(vlarge) mc(blue%50) mlc(black) mlw(vthin)   ///
|| scatter price mpg if foreign==1 , msiz(vlarge) mc(red%50)  mlc(black) mlw(vthin)    ///
, legend(order(1 "Domestic" 2 "Foreign") region(lcolor(%0)))                          ///
  graphr(color(white)) plotr(color(white))  name(g2,replace) 

graph combine g1 g2, xsize(20) ysize(10) graphr(color(white)) plotr(color(white))

Deuxième graphique avec un seul élément

gen price0 = price if  foreign==0
gen price1 = price if  foreign==1

tw scatter price0 price1 mpg  ///
, msiz(large large) mc(red%50 blue%50) mlc(black black) mlw(vthin vthin) ///
  legend(order(1 "Domestic" 2 "Foreign"))  name(g2,replace)  jitter(5)

2 [Quelques] Options graphiques

2.1 Graph 3

grstyle init
grstyle set legend, nobox stack
grstyle set plain, horizontal grid compact
grstyle set mesh,  horizontal

*** Scatter
clear
set obs 200
gen x1=runiform(0,12)
gen y1=runiform(0,8)
gen x2=runiform(8,20)
gen y2=runiform(12,20)

******************************************
* Sans modification du contour
local o1  mc("248 118 109")      msiz(large)
local o2  mc("0 191 196")        msiz(large)
local o3  mc("248 118 109%50")   msiz(large)
local o4  mc("0 191 196%50")     msiz(large)

tw (scatter y2 x1, `o1')  ///
|| (scatter y2 x2, `o2')  ///
|| (scatter y1 x1, `o3')  ///
|| (scatter y1 x2, `o4')  ///
,  legend(off)  graphr(color(white)) plotr(color(white)) nodraw name(g1,replace)
******************************************
* Avec modification du contour
local o1  mc("248 118 109*0.5") msiz(large) mlc(black) mlw(vthin) msiz(large)
local o2  mc("0 191 196*0.5")   msiz(large) mlc(black) mlw(vthin) msiz(large)
local o3  mc("248 118 109%50")  msiz(large) mlc(black) mlw(vthin) msiz(large)
local o4  mc("0 191 196%50")    msiz(large) mlc(black) mlw(vthin) msiz(large)

tw (scatter y2 x1, `o1')  ///
|| (scatter y2 x2, `o2')  ///
|| (scatter y1 x1, `o3')  ///
|| (scatter y1 x2, `o4')  ///
,  legend(off)  graphr(color(white)) plotr(color(white)) nodraw name(g2,replace) 

graph combine g1 g2, graphr(color(white)) plotr(color(white))  ///
note("{bf:Nord-Ouest}: par défaut" ///
     "{bf:Nord-Est}:  contour noir et réduction de l'intensité *.5" ///
     "{bf:Sud-Ouest}: contour par défaut et réduction de l'opacité %50 " ///
     "{bf:Sud-Est}:    contour noir et réduction de l'opacité %50") ///
title("Intensité - Opacité - Couleur du contour")    

2.2 Graph 4

* suite du programme précédent

forval i=3/11 {
gen y`i'= y`i-1'  
}

graph bar y2 y3 y4,                                                                          ///
bar(1,fcolor("33 144 140") lc(white))                                                        ///
bar(2,fcolor("33 144 140*.5") lc(white))                                                     ///
bar(3,fcolor("33 144 140%50") lc(white))                                                     ///
legend(off) graphr(color(white)) plotr(color(white))                                         ///
note("{bf:Barre1}: Defaut" "{bf:Barre2}: *.5 d'intensité" "{bf:Barre3}: %50 de transparence" 

2.3 Graph 5

* suite du programme précédent

graph bar y2 y3 y4 y5 y6 y7 y8 y9 y10 y11 , ///
bar(1,fcolor(68 1 84) lc(white)) ///
bar(2,fcolor(33 144 140*.9)  lc(white)) ///
bar(3,fcolor(68 1 84*.8)     lc(white)) ///
bar(4,fcolor(33 144 140*.7)  lc(white)) ///
bar(5,fcolor(68 1 84*.6)     lc(white)) ///
bar(6,fcolor(33 144 140*.5)  lc(white)) ///
bar(7,fcolor(68 1 84*.4)     lc(white)) ///
bar(8,fcolor(33 144 140*.3)  lc(white)) /// 
bar(9,fcolor(68 1 84*.2)     lc(white)) ///
bar(10,fcolor(33 144 140*.1) lc(white)) ///
legend(pos(3) col(1)  ///
order(1 "*1" 2 "*.9" 3 "*.8" 4 "*.7" 5 "*.6" 6 "*.5" 7 "*.4" 8 "*.3" 9 "*.2" 10 "*.1" )) ///
title("Reduction de l'intensité") name(g1,replace) nodraw ylabel(0(2)16)

* transparence
graph bar y2 y3 y4 y5 y6 y7 y8 y9 y10 y11 , ///
bar(1,fcolor(68 1 84)        lc(white)) ///
bar(2,fcolor(33 144 140%90)  lc(white)) ///
bar(3,fcolor(68 1 84%80)     lc(white)) ///
bar(4,fcolor(33 144 140%70)  lc(white)) ///
bar(5,fcolor(68 1 84%60)     lc(white)) ///
bar(6,fcolor(33 144 140%50)  lc(white)) ///
bar(7,fcolor(68 1 84%40)     lc(white)) ///
bar(8,fcolor(33 144 140%30)  lc(white)) /// 
bar(9,fcolor(68 1 84%20)     lc(white)) ///
bar(10,fcolor(33 144 140%10) lc(white)) ///
legend(pos(3) col(1)  ///
order(1 "%10" 2 "%90" 3 "%80" 4 "%70" 5 "%60" 6 "%50" 7 "%40" 8 "%30" 9 "%20" 10 "%10" )) ///
title("Reduction de l'opacité") name(g2,replace) nodraw ylabel(0(2)16)

graph combine g1 g2

2.4 Graph 6

clear
set obs 12
gen x=_n 
gen y=_n 

grstyle init
grstyle set plain, horizontal grid compact
grstyle set mesh,  horizontal
grstyle set color hue, n(12)

tw scatter y x in 1, msize(vtiny)    mlc(black) mlw(vthin)   ///
|| scatter y x in 2, msize(tiny)     mlc(black) mlw(vthin)   /// 
|| scatter y x in 3, msize(vsmall)   mlc(black) mlw(vthin)   ///
|| scatter y x in 4, msize(small)    mlc(black) mlw(vthin)   ///
|| scatter y x in 5, msize(medsmall) mlc(black) mlw(vthin)   ///
|| scatter y x in 6, msize(medium)   mlc(black) mlw(vthin)   ///
|| scatter y x in 7, msize(medlarge) mlc(black) mlw(vthin)   ///
|| scatter y x in 8, msiz(large)     mlc(black) mlw(vthin)   ///
|| scatter y x in 9, msiz(vlarge)    mlc(black) mlw(vthin)   ///
|| scatter y x in 10, msiz(huge)     mlc(black) mlw(vthin)   ///
|| scatter y x in 11, msiz(vhuge)    mlc(black) mlw(vthin)   ///
|| scatter y x in 12, msiz(ehuge)    mlc(black) mlw(vthin)   ///
,legend(off) xtitle("") ytitle("")                           ///
xlab(, nogrid labc(%0))                                                            ///
ylab(1 "vtiny" 2 "tiny" 3 "vsmall" 4  "small" 5 "medsmall" 6 "medium" 7 "medlarge" ///
     8 "large" 9 "vlarge" 10 "huge" 11 "vhuge" 12 "ehuge")  scale(.9)              ///
title("Circle marker size" "{stMono:msize() msiz()}",  pos(11))

2.5 Graph 7

* suite du programme précédent

local m circle diamond triangle square               
foreach m2 of local m {
tw scatter y x in 1, msize(vtiny)    mlc(black) mlw(vthin) m(`m2')   ///
|| scatter y x in 2, msize(tiny)     mlc(black) mlw(vthin) m(`m2')   ///
|| scatter y x in 3, msize(vsmall)   mlc(black) mlw(vthin) m(`m2')   ///
|| scatter y x in 4, msize(small)    mlc(black) mlw(vthin) m(`m2')   ///
|| scatter y x in 5, msize(medsmall) mlc(black) mlw(vthin) m(`m2')   ///
|| scatter y x in 6, msize(medium)   mlc(black) mlw(vthin) m(`m2')   ///
|| scatter y x in 7, msize(medlarge) mlc(black) mlw(vthin) m(`m2')   ///
|| scatter y x in 8, msiz(large)     mlc(black) mlw(vthin) m(`m2')   ///
|| scatter y x in 9, msiz(vlarge)    mlc(black) mlw(vthin) m(`m2')   ///
|| scatter y x in 10, msiz(huge)     mlc(black) mlw(vthin) m(`m2')   ///
|| scatter y x in 11, msiz(vhuge)    mlc(black) mlw(vthin) m(`m2')   ///
|| scatter y x in 12, msiz(ehuge)    mlc(black) mlw(vthin) m(`m2')   /// 
,legend(off) xtitle("") ytitle("")                                                 ///
xlab(, nogrid labc(%0))                                                            ///
ylab(1 "vtiny" 2 "tiny" 3 "vsmall" 4  "small" 5 "medsmall" 6 "medium" 7 "medlarge" ///
     8 "large" 9 "vlarge" 10 "huge" 11 "vhuge" 12 "ehuge")  scale(.9)              ///
title("{stMono: msymbol(`m2')}", pos(11)) name("`m2'", replace) nodraw 
}
graph combine circle diamond triangle square, cols(2)          ///
title("Solid marker size" "{stMono:msize() msiz()}", pos(11))

2.6 Graph 8

* suite du programme précédent
          
local m  m plus X arrowf arrow pipe V                      
foreach m2 of local m {
tw scatter y x in 1, msize(vtiny)    mlw(medthin) m(`m2')   ///
|| scatter y x in 2, msize(tiny)     mlw(medthin) m(`m2')   ///   
|| scatter y x in 3, msize(vsmall)   mlw(medthin) m(`m2')   ///   
|| scatter y x in 4, msize(small)    mlw(medthin) m(`m2')   ///   
|| scatter y x in 5, msize(medsmall) mlw(medthin) m(`m2')   ///
|| scatter y x in 6, msize(medium)   mlw(medthin) m(`m2')   ///
|| scatter y x in 7, msize(medlarge) mlw(medthin) m(`m2')   ///
|| scatter y x in 8, msiz(large)     mlw(medthin) m(`m2')   ///
|| scatter y x in 9, msiz(vlarge)    mlw(medthin) m(`m2')   ///
|| scatter y x in 10, msiz(huge)     mlw(medthin) m(`m2')   ///
|| scatter y x in 11, msiz(vhuge)    mlw(medthin) m(`m2')   ///
|| scatter y x in 12, msiz(ehuge)    mlw(medthin) m(`m2')   ///
,legend(off) xtitle("") ytitle("") ///
xlab(, nogrid labc(%0))           ///
ylab(1 "vtiny" 2 "tiny" 3 "vsmall" 4  "small" 5 "medsmall" 6 "medium" 7 "medlarge" ///
     8 "large" 9 "vlarge" 10 "huge" 11 "vhuge" 12 "ehuge")  scale(.9)              ///
title("{stMono: msymbol(`m2')}", pos(11)) name("`m2'", replace) nodraw 
}
graph combine plus X arrowf arrow pipe V   , cols(2)  ///
title("Symbol marker size" "{stMono:msize() msiz()}", pos(11) size(medium)) ///
note("Epaisseur du trait:{stMono: mlw(medthin)}")

2.7 Graph 9

* suite du programme précédent

local m  m circle_hollow  diamond_hollow triangle_hollow  square_hollow                     
foreach m2 of local m {
tw scatter y x in 1, msize(vtiny)    mlw(medthin) m(`m2')   ///
|| scatter y x in 2, msize(tiny)     mlw(medthin) m(`m2')   ///   
|| scatter y x in 3, msize(vsmall)   mlw(medthin) m(`m2')   ///   
|| scatter y x in 4, msize(small)    mlw(medthin) m(`m2')   ///   
|| scatter y x in 5, msize(medsmall) mlw(medthin) m(`m2')   ///
|| scatter y x in 6, msize(medium)   mlw(medthin) m(`m2')   ///
|| scatter y x in 7, msize(medlarge) mlw(medthin) m(`m2')   ///
|| scatter y x in 8, msiz(large)     mlw(medthin) m(`m2')   ///
|| scatter y x in 9, msiz(vlarge)    mlw(medthin) m(`m2')   ///
|| scatter y x in 10, msiz(huge)     mlw(medthin) m(`m2')   ///
|| scatter y x in 11, msiz(vhuge)    mlw(medthin) m(`m2')   ///
|| scatter y x in 12, msiz(ehuge)    mlw(medthin) m(`m2')   ///
,legend(off) xtitle("") ytitle("") ///
xlab(, nogrid labc(%0))           ///
ylab(1 "vtiny" 2 "tiny" 3 "vsmall" 4  "small" 5 "medsmall" 6 "medium" 7 "medlarge" ///
     8 "large" 9 "vlarge" 10 "huge" 11 "vhuge" 12 "ehuge")  scale(.9)              ///
title("{stMono: msymbol(`m2')}", pos(11)) name("`m2'", replace) nodraw 
}
graph combine circle_hollow  diamond_hollow triangle_hollow  square_hollow , cols(2)  ///
title("Hollow marker size" "{stMono:msize() msiz()}", pos(11)) scale(.9)              ///
note("Epaisseur du trait:{stMono: mlw(medthin)}", size(vsmall))

2.8 Graph 10

* suite du programme précédent

colorpalette hue, n(10) nograph  reverse 
tw scatter y x in 1, mc(%0) mlc(%0) ylab(0(1)10) ///
yline(1, lc(`r(p1)') lw(vvthin))                 ///
yline(2, lc(`r(p2)') lw(vthin))                  ///
yline(3, lc(`r(p3)') lw(thin))                   ///
yline(4, lc(`r(p4)') lw(medthin))                ///
yline(5, lc(`r(p5)') lw(medium))                 ///
yline(6, lc(`r(p6)') lw(medthick))               ///
yline(7, lc(`r(p7)') lw(thick))                  /// 
yline(8, lc(`r(p8)') lw(vthick))                 /// 
yline(9, lc(`r(p9)') lw(vvthick))                /// 
yline(10, lc(`r(p10)') lw(vvvthick))             /// 
ylab(1 "vvthin" 2 "vthin" 3 "thin" 4  "medthin" 5 "medium" 6 "medthick" 7 "thick" ///
     8 "vthick" 9 "vvthick" 10 "vvvthick") xtitle("") ytitle("")                  ///
title("line width" "{stMono:lwidth() lw()}", pos(11)) xlab(, nogrid labc(%0))    

2.9 Graph 11

* suite du programme précédent

local c solid  dash  dot  dash_dot  shortdash shortdash_dot longdash longdash_dot  
foreach c2 of local c {
colorpalette viridis, n(10) nograph  reverse
tw scatter y x in 1, mc(%0) mlc(%0) ylab(0(1)10) ///
yline(1, lp(`c2') lc(`r(p1)') lw(vvthin))        ///
yline(2, lp(`c2') lc(`r(p2)') lw(vthin))         ///
yline(3, lp(`c2') lc(`r(p3)') lw(thin))          ///
yline(4, lp(`c2') lc(`r(p4)') lw(medthin))       ///
yline(5, lp(`c2') lc(`r(p5)') lw(medium))        ///
yline(6, lp(`c2') lc(`r(p6)') lw(medthick))      ///
yline(7, lp(`c2') lc(`r(p7)') lw(thick))         /// 
yline(8, lp(`c2') lc(`r(p8)') lw(vthick))        /// 
yline(9, lp(`c2') lc(`r(p9)') lw(vvthick))       /// 
yline(10,lp(`c2') lc(`r(p10)') lw(vvvthick))     ///                        
ylab(1 "vvthin" 2 "vthin" 3 "thin" 4  "medthin" 5 "medium" 6 "medthick" 7 "thick"                     ///
     8 "vthick" 9 "vvthick" 10 "vvvthick") xtitle("")                                                 ///
xlab(, nogrid labc(%0)) title("{stMono: lp(`c2')}", pos(11)) name("`c2'", replace) nodraw ytitle("")
} 

graph combine solid  dash  dot  dash_dot shortdash shortdash_dot longdash longdash_dot, cols(4) ///
title("Pattern line width: {stMono:lwidth() - lw()}", pos(11))  scale(.8)

2.10 Graph 12

* suite du programme précédent

gen z="TEXT" 
gen x2=1

grstyle init
grstyle set plain, horizontal grid compact
grstyle set mesh,  horizontal
grstyle set color matplotlib, jet n(11) reverse

tw scatter y x in 1,  mlabel(z)  mlabs(minuscule) mc(%0)  ///
|| scatter y x in 2,  mlabel(z)  mlabs(tiny)      mc(%0)  ///
|| scatter y x in 3,  mlabel(z)  mlabs(vsmall)    mc(%0)  ///
|| scatter y x in 4,  mlabel(z)  mlabs(small)     mc(%0)  ///
|| scatter y x in 5,  mlabel(z)  mlabs(medsmall)  mc(%0)  ///
|| scatter y x in 6,  mlabel(z)  mlabs(medium)    mc(%0)  ///
|| scatter y x in 7,  mlabel(z)  mlabs(medlarge)  mc(%0)  ///
|| scatter y x in 8,  mlabel(z)  mlabs(large)     mc(%0)  ///
|| scatter y x in 9,  mlabel(z)  mlabs(vlarge)    mc(%0)  ///
|| scatter y x in 10, mlabel(z)  mlabs(huge)      mc(%0)  ///
|| scatter y x in 11, mlabel(z)  mlabs(vhuge)     mc(%0)  ///
legend(off) xlab(0(1)16) ///
ylab(1 "minuscule" 2 "tiny"  3"vsmall" 4 "small" 5  "medsmall" 6 "medium" 7 "medlarge" 8 "large" ///
9 "vlarge" 10 "huge" 11 "vhuge" )                                                                ///
xlab(, nogrid  labc(%0)) xtitle("") ytitle("")                                                   ///
title("Text size: {stMono:size()}", pos(11))                                                     ///
note("Option pour {stMono:title() legend() note() caption() mlabel()}", size(small))

2.11 Graph 13

sysuse auto, clear

qui separate mpg, by(foreign)

kdensity mpg0 , g(x1 d1) nograph k(gauss) bw(2)
kdensity mpg1 , g(x0 d0) nograph k(gauss) bw(2)
 
tw histogram mpg0  , bin(10)   fcolor("72 27 109%60")  lc(black) lw(vvthin)                 ///
|| histogram mpg1  , bin(10)   fcolor("207 225 28%60") lc(black) lw(vvthin)                 ///
legend(order(1 "Domestic" 2 "Foreign") row(1)  region(lcolor(%0)))                          /// 
xlabel(10(10)50)  name(g1, replace)

tw area d1 d0 x0, fc("72 27 109%60" "207 225 28%60") lc(black black) lw(vthin vthin)                 ///
legend(order(1 "Domestic" 2 "Foreign") row(1) region(lcolor(%0))) name(g2,replace) xlabel(10(10)50)  ///
xtitle("")  ytitle("Density") 

grc1leg g1 g2, leg(g1) pos(11) row(2)  xsize(10) ysize(20)

2.12 Graph 14

* Suite du programme précédent

tw  lfitci  price mpg  if foreign==0,  clcolor(blue*1.2) fcolor(blue%10)       alcolor(%0)   ///
 || lfitci  price mpg  if foreign==1,  clcolor(red*1.2)  fcolor(red%10)        alcolor(%0)   ///
 || scatter price mpg  if foreign==0,  msize(medlarge)   mlc(black) mlw(vthin) mc(blue*.5)   ///
 || scatter price mpg  if foreign==1,  msize(medlarge)   mlc(black) mlw(vthin) mc(red*.5)    ///
 , legend(order(5 "Domestic" 6 "Foreign"))  ///                                              ///
    ytitle("price") xtitle("mpg")         

2.13 Graph 15

* nord sud exterieur
local col1 34 94 168%80
local col2 127 205 187%80

local l 11 12 1 7 6 5 
foreach l2 of local l { 
tw function y=normalden(x),     range(-5 5) color("`col1'")   lc(%0) recast(area)     ///
|| function y=logisticden(x),   range(-5 5) color("`col2'")   lc(%0) recast(area)     ///
plotregion(style(none)) ysca(off) xsca(noline) legend(off)  xtitle("")              ///
title("{stMono:pos(`l2') ring(1)}", pos(`l2') ring(1)) nodraw name(g`l2', replace) 
}
graph combine g11 g12 g1 g7 g6 g5, title("Exterieur", pos(11)) name(nse, replace)  


* nord sud interieur
local l 11 12 1 7 6 5 
foreach l2 of local l { 
tw function y=normalden(x),     range(-5 5) color("`col1'")   lc(%0) recast(area)    ///
|| function y=logisticden(x),   range(-5 5) color("`col2'")  lc(%0) recast(area)    ///
plotregion(style(none)) ysca(off) xsca(noline) legend(off)  xtitle("")             ///
title("{stMono:pos(`l2') ring(0)}", pos(`l2') ring(0)) name(g`l2', replace) nodraw
}
graph combine g11 g12 g1 g7 g6 g5, title("Intérieur", pos(11)) nodraw  name(nsi, replace)  


graph combine nsi nse, col(1) xsize(15) ysize(20)  

2.14 Graph 16

* est ouest exterieur
local l 2 3 4 8 9 10 
foreach l2 of local l { 
tw function y=normalden(x), range(-5 5) color(red%30)  lc(%0) recast(area)                       ///
|| function y=logisticden(x),   range(-5 5) color(blue%30)  lc(%0) recast(area)                  ///
   plotregion(style(none)) ysca(off) xsca(noline) legend(off) xtitle("")                         ///
title("{stMono:pos(`l2')}" "{stMono:ring(1)}", pos(`l2') ring(1)) name(g`l2', replace) nodraw
}

graph combine g10 g2 g9 g3 g8 g4  , col(2)  title("Exterieur", pos(11))  nodraw name(eoe, replace)  

 
 * est ouest intérieur
local l 2 3 4 8 9 10 
foreach l2 of local l { 
tw function y=normalden(x), range(-5 5) color(red%30)  lc(%0) recast(area)                     ///
|| function y=logisticden(x),   range(-5 5) color(blue%30)  lc(%0) recast(area)                ///
   plotregion(style(none)) ysca(off) xsca(noline) legend(off) xtitle("")                       ///
title("{stMono:pos(`l2')}" "{stMono:ring(1)}", pos(`l2') ring(0)) name(g`l2', replace) nodraw
}

graph combine g10 g2 g9 g3 g8 g4  , col(2) title("Intérieur", pos(11)) nodraw name(eoi, replace)  
 
graph combine eoi eoe, col(1)  xsize(15) ysize(20)

2.15 Graph 16

sysuse lifeexp, clear

grstyle init
grstyle set plain, horizontal grid compact
grstyle set imesh,  horizontal
grstyle set color plasma, n(3) reverse


local l legend(order(1 "Eur & C.Asia" 2 "N.A" 3 "S.A") col(3) region(lc(%0))) 
local o msiz(medium) mc(%70) mlc(black) mlw(vthin) jitter(2)
tw scatter lexp gnppc if region==1, `o'  ///
|| scatter lexp gnppc if region==2, `o'  ///
|| scatter lexp gnppc if region==3, `o'  ///
,  title("Par défaut") `l'  nodraw name(xy1,replace) 

local l legend(order(1 "Eur & C.Asia" 2 "N.A" 3 "S.A") col(3) region(lc(%0))) 
local o msiz(medium) mc(%70) mlc(black) mlw(vthin) jitter(2)
tw scatter lexp gnppc if region==1, `o'   ///
|| scatter lexp gnppc if region==2, `o'   ///
|| scatter lexp gnppc if region==3, `o'   ///
,  title("Options1") `l'                  ///
   xscale(log) xlabel(,alt)  ytitle(, size(small)) xtitle(, size(small)) nodraw name(xy2,replace)

local l legend(order(1 "Eur & C.Asia" 2 "N.A" 3 "S.A") col(3) region(lc(%0))) 
local o msiz(medium) mc(%70) mlc(black) mlw(vthin) jitter(2)
tw scatter lexp gnppc if region==1, `o'   ///
|| scatter lexp gnppc if region==2, `o'   ///
|| scatter lexp gnppc if region==3, `o'   ///
,  title("Options 2") `l'   ///
   xscale(log) xlabel(10000 "10k" 20000 "20k" 30000 "30k" 40000 "40k") ///
   ylabel(50(5)80) ytitle(, size(small)) xtitle(, size(small)) nodraw name(xy3,replace)
   
graph twoway scatteri 1 1 "No Data", mlabsize(*0) mlabpos(0) msym(i) ///
yscale(off noline) xscale(off noline)                                ///
plotregion(style(none))                                              ///
note("{bf: Options 1}:"                                              ///
     "   - Axe X (revenu) en échelle log"                            ///
     "   {stMono: xscale(log)}"                                        ///
     "   - Positions label axe X alterneés "                         ///
     "   {stMono: xslabel(alt)}"                                       ///
     "   - Réduction de la taille du titre  des axes"                  ///
     "   {stMono: xtitle(size(small)) ytitle(size(small))}"            ///
     " "                                                               ///
     "{bf: Options 2}:"                                                ///
     "   - Minimum de l'axe Y à 50 ans"                              ///
     "   {stMono: ylabel(50(5)80)}"                                    ///  
     "   - Modification manuelle des labels de l'axe X"                ///
     "   {stMono: xlabel(10000 '10k' 20000 '20k'   /// }"              ///
     "                   {stMono:30000 '30k' 40000 '40k')}"            ///       
, pos(9) ring(0)) nodraw name(xy4,replace)
     
graph combine xy1 xy2 xy3 xy4,  iscale(.7) title(""Espérance de vie et revenu" ")

2.16 Graph 17

grstyle init
grstyle set plain, horizontal grid compact
grstyle set imesh,  horizontal
grstyle set color viridis, n(2) reverse

sysuse auto, clear

local ops mcolor(%70) mlc(black) mlw(vthin)

tw scatter   mpg price if foreign==0, `ops'   ///
|| scatter   mpg price if foreign==1, `ops'   ///
,  title("TITRE", pos(11))                    ///
   subtitle("Sous Titre", pos(11))            ///
   note("NOTE", pos(6), )                     ///
   caption("Caption", pos(5))                 ///
   legend(order(1 "{bf:Domestic}" 2 "{bf:Foreign}")  pos(1) ring(0) col(1) region(lw(vvthin) fc(%50)) stack)  ///
   text(10000 30 "TEXT: Y=10000 X=30") 
   
local ops mcolor(%70) mlc(black) mlw(vthin) msize(large) jitter(3) 
tw scatter   mpg price if foreign==0, `ops'         ///
|| scatter   mpg price if foreign==1, `ops'         ///
title("Legende par défaut") nodraw name(l1, replace)

tw scatter   mpg price if foreign==0, `ops'         ///
|| scatter   mpg price if foreign==1, `ops'         ///
,  title("Modification de la légende")              ///
   legend(order(1 "{bf:Domestic}" 2 "{bf:Foreign}") ///
   pos(1) ring(0) col(1) ///
   region(lw(vvthin)) ///
   stack)  nodraw name(l2, replace)
   
graph combine l1 l2, xsize(20) ysize(10)  

2.17 Exemples graph combine

Exemple 1

clear
set obs 100

* 100 %
local g "scatteri 1 1,  ylab(,nogrid) xlab(,nogrid) mc(%0) xtitle("") ytitle("") yscale(off noline) xscale(off noline)"

tw `g' plotr(color("68 1 84")) graphr(color("68 1 84"))                             ///
    title("{bf: Graphique 1}", ring(0) pos(0) color(gs13)) name(g1, replace) nodraw
tw `g' plotr(color("49 104 142")) graphr(color("49 104 142"))                       ///
    title("{bf: Graphique 2}", ring(0) pos(0) color(gs10)) name(g2, replace) nodraw
tw `g' plotr(color("53 183 121")) graphr(color("53 183 121"))                       ///
    title("{bf: Graphique 3}", ring(0) pos(0) color(gs5)) name(g3, replace) nodraw
tw `g' plotr(color("253 231 37")) graphr(color("253 231 37"))                       ///
    title("{bf: Graphique 4}", ring(0) pos(0) color(gs2)) name(g4, replace) nodraw
graph combine g1 g2 g3 g4, graphr(color(white))

local g "scatteri 1 1,  ylab(,nogrid) xlab(,nogrid) mc(%0) xtitle("") ytitle("") yscale(off noline) xscale(off noline)"
local fxy1 fxsize(100) fysize(25)
local fxy2 fxsize(25) fysize(25)
local fxy3 fxsize(100) fysize(100)
local fxy4 fxsize(25) fysize(100)

local t1 title("{bf:fxsize(100) fysize(25)}",          ring(0) pos(0) color(gs13))
local t2 title("{bf:fxsize(25)}" "{bf: fysize(25)}",   ring(0) pos(0) color(gs10))
local t3 title("{bf:fxsize(100) fysize(100)}",         ring(0) pos(0) color(gs5))
local t4 title("{bf:fxsize(25)}" "{bf:fysize(100)}" ,  ring(0) pos(0) color(gs2))

tw `g' `t1' plotr(color("68 1 84"))    graphr(color("68 1 84"))    name(g1, replace) nodraw `fxy1'
tw `g' `t2' plotr(color("49 104 142")) graphr(color("49 104 142")) name(g2, replace) nodraw `fxy2'
tw `g' `t3' plotr(color("53 183 121")) graphr(color("53 183 121")) name(g3, replace) nodraw `fxy3'
tw `g' `t4' plotr(color("253 231 37")) graphr(color("253 231 37")) name(g4, replace) nodraw `fxy4'

graph combine g1 g2 g3 g4, plotr(color(white)) graphr(color(white))

clear
set obs 1000
gen x = rnormal() +10
gen y = rnormal() +10
kdensity x, gen(xx dx) kernel(gauss) nograph n(200) bw(1)
kdensity y, gen(yy dy) kernel(gauss) nograph n(200) bw(1)


local g "ylab(,nogrid) xlab(,nogrid) mc(%0) xtitle("") ytitle("") yscale(off noline) xscale(off noline)"
local fxy1 fxsize(100) fysize(25)
local fxy2 fxsize(25)  fysize(25)
local fxy3 fxsize(100) fysize(100)
local fxy4 fxsize(25)  fysize(100)


local t2 note("{bf:fxsize(25)}" "{bf:fysize(25)}", color(gs10) size(medium))

tw area dx xx, `g' fc("253 231 37") lw(thin) lc(white) ///
   plotr(color("68 1 84"))    graphr(color("68 1 84"))    name(g1, replace) nodraw `fxy1'
tw area yy dy, `g' fc("253 231 37") lw(thin) lc(white) ///
   plotr(color("49 104 142")) graphr(color("49 104 142")) name(g4, replace) nodraw `fxy4'   
tw scatteri 1 1, `g'  plotr(color("white")) graphr(color("white")) ///
   title("{bf:Le graphique}" "{bf:du}" "{bf:dimanche}" , pos(0) ring(0)) name(g2, replace) nodraw `fxy2'
tw scatter y x, `g'   mc("253 231 37") mlc(black) mlw(vthin) ///
   plotr(color("53 183 121")) graphr(color("53 183 121")) name(g3, replace) nodraw `fxy3'   
tw scatter y x, `g'   mc("253 231 37") mlc(black) mlw(vthin) ///
   plotr(color("53 183 121")) graphr(color("53 183 121")) name(g3, replace) nodraw `fxy3'   


graph combine g1 g2 g3 g4, graphr(color(white) margin(0 0 0 0))

3 Macros

3.1 Graph 18 & 19

  • Quelques légères modifications niveau couleurs par rapport au graphique généré, mais qui permettent d’utiliser directement une palette de couleur par défaut (YlGnBu) avec des macros [voir chapitre suivant]
  • On peut modifier facilement l’apparence du graphique en utilisant d’autres palettes de la collection Colorbrewer [idéalement à 9 couleurs]
    • on change le nom de la palette sur la ligne local palette YlGnBu
    • on peut changer de collection facilement en modifiant les deux lignes avec colorpalette et grstyle set color
* pour local varname rep

sysuse auto, clear
gen rep= rep78<4
label define rep 0 "rep<4" 1 "rep>=4", modify
label value rep rep

local varname rep
local labn: value label `varname'
levelsof `varname', local(l)
foreach l2 of local l {
local lab`l2': label `labn'  `l2'
}

local palette YlGnBu 

colorpalette `palette', select(9) intensity(2.2) nograph
local bcol `r(p)'
local mopt mlc(black) mlw(vvthin) msiz(medlarge) jitter(2)

grstyle init
grstyle set plain, horizontal grid compact
grstyle set mesh,  horizontal
grstyle set color `palette', select(2 4 5)

tw scatter price mpg if `varname'==1, mc("`r(p1)'")  `mopt'                            ///
|| scatter price mpg if `varname'==0, mc("`r(p3)'")  `mopt'                            ///
, legend(order(1 "`lab0'" 2 "`lab1'") pos(7) color("`r(p2)'") region(color("`bcol'"))) ///
ytitle(, color("`r(p2)'"))                                                             ///
xtitle(, color("`r(p2)'"))                                                             ///
ylabel(, labc("`r(p2)'") glw(vvthin))                                                  ///
xlabel(, labc("`r(p2)'") glw(vvthin))                                                  ///
plotr(color("`bcol'"))   graphr(color("`bcol'"))                                       ///
title("local varname rep", pos(11) color("`r(p2)'")) 

3.2 Graph 20

  • Mêmes remùarques que pour le graphique précédent
* suite du programme précédent

xtile qprice = price, n(4)
label define qp 1 "<Q25" 2 "Q25-Q50" 3 "Q50-Q75" 4 ">Q75", modify
label value qprice qp

local varname qprice
local labn: value label `varname'
levelsof `varname', local(l)
foreach l2 of local l {
local lab`l2': label `labn'  `l2'
}

local palette    YlGnBu 

colorpalette YlGnBu, select(9) intensity(2.2) nograph
local bcol `r(p)'

local varlist price mpg
foreach v of loc varlist {
qui sum `v'
local  m`v' : di %6.2f `r(mean)'
}

grstyle init
grstyle set plain, horizontal grid compact
grstyle set mesh,  horizontal
grstyle set color `palette', n(15) select(10 8 6 4 2)

tw scatter price mpg if qprice==1, mlc("`bcol'") mlw(.2pt) mc("`r(p1)'")  msiz(medlarge)  jitter(2)      ///
|| scatter price mpg if qprice==2, mlc("`bcol'") mlw(.2pt) mc("`r(p2)'")  msiz(medlarge)  jitter(2)      ///
|| scatter price mpg if qprice==3, mlc("`bcol'") mlw(.2pt) mc("`r(p3)'")  msiz(medlarge)  jitter(2)      ///
|| scatter price mpg if qprice==4, mlc("`bcol'") mlw(.2pt) mc("`r(p4)'")  msiz(medlarge)                 ///
|| , legend(order(4 "`lab4'" 3 "`lab3'" 2 "`lab2'" 1 "`lab1'")                                           ///
            col(1) pos(9)  color("`r(p3)'") region(color("`bcol'")) size(vsmall) )                       ///
yline(`mprice', lw(vthin) lc("`r(p5)'") noext)                                                           ///
xline(`mmpg', lw(vthin) lc("`r(p5)'") noext)                                                             ///
ytitle(, color("`r(p3)'"))                                                                               ///
xtitle(, color("`r(p3)'"))                                                                               ///
ylabel(,  labc("`r(p3)'")  glw(vvthin))                                                                  ///
xlabel(,  labc("`r(p3)'")  glw(vvthin))                                                                  ///
plotr(color("`bcol'"))     graphr(color("`bcol'"))                                                       ///
note("Moyenne Price = `mprice'"                                                                          ///
     "Moyenne Mpg   = `mmpg'", color("`r(p3)'") pos(7))  

3.3 Graph 21

  • On récupère les coordonnées de x avec la commande sum.
    • Rappel: les commandes pci et scatteri entrent directement les coordonnées et non via des variables
  • On pourrait facilement automatiser l’application des coordonnées de y avec des macros (ici valeurs 2 à 5)
  • Le coude entre la moyenne d’ensemble et les droites pour les moyennes des valeurs extrêmes de rep78 à demandé de légèrement ajuster les coordonnées (1.986 au lieu de 2 et 5.014 au lieu de 5).
  • Si le nombre d’observations était plus élevé, il faudrait plutôt mettre le nuage de point en arrière plan par rapport aux lollipops
graph set window fontface Consolas

recode rep78 (1=2)  

tokenize displacement rep78

qui sum `1' 
local mt `r(mean)'

local i=1
levelsof `2', local(l)
foreach l2 of local l {
qui sum `1'   if rep78==`l2'
local m`i++'  `r(mean)'
}

local palette    YlGnBu 

colorpalette YlGnBu, select(9) intensity(2.2) nograph
local bcol `r(p)'

local tcol "127 205 180"

grstyle init
grstyle set plain, horizontal grid compact
grstyle set mesh,  horizontal
grstyle set color `palette', n(20)  select(16 13 10 7 5)

tw ///
|| pci       2 `mt' 2 `m1' , lc("`r(p5)'")    lw(thick)                                      ///
|| scatteri  2 `m1'        , mc("`r(p1)'")    msiz(large) mlc("`r(p5)'") mlw(thin)           ///
|| pci       3 `mt' 3 `m2' , lc("`r(p5)'")    lw(thick)                                      ///
|| scatteri  3 `m2'        , mc("`r(p2)'")    msiz(large) mlc("`r(p5)'") mlw(thin)           ///
|| pci       4 `mt' 4 `m3' , lc("`r(p5)'")    lw(thick)                                      ///
|| scatteri  4 `m3'        , mc("`r(p3)'")    msiz(large) mlc("`r(p5)'") mlw(thin)           ///
|| pci       5 `mt' 5 `m4' , lc("`r(p5)'")    lw(thick)                                      ///
|| scatteri  5 `m4'        , mc("`r(p4)'")    msiz(large) mlc("`r(p5)'") mlw(thin)           ///
|| scatter `2' `1' if `2' ==2, mc("`r(p1)'")  msiz(small) mlc("`bcol'") mlw(vthin) jitter(6) ///
|| scatter `2' `1' if `2' ==3, mc("`r(p2)'")  msiz(small) mlc("`bcol'") mlw(vthin) jitter(6) ///
|| scatter `2' `1' if `2' ==4, mc("`r(p3)'")  msiz(small) mlc("`bcol'") mlw(vthin) jitter(6) ///
|| scatter `2' `1' if `2' ==5, mc("`r(p4)'")  msiz(small) mlc("`bcol'") mlw(vthin) jitter(6) ///
|| pci 1.986 `mt' 5.014 `mt', lw(thick) lc("`r(p5)'")                                        ///
|| , legend(off)                                                                             ///
xlabel(,glw(vvthin) labcolor("`tcol'"))                                                      ///
ylabel(,glw(vvthin) labcolor("`tcol'"))                                                      ///
xtitle("`var'", color("`tcol'"))                                                             ///
ytitle("rep78", color("`tcol'"))                                                             ///
title("Averages of `1'", pos(11) color("`tcol'"))                                            ///
plotr(color("`bcol'"))   graphr(color("`bcol'"))                                                    

3.4 Graph 23